package com.lsbc.pay.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 
 * @author yiyang
 *
 * @date: 2019年3月13日
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
	private boolean enableSwagger = true;

	@Bean
	public Docket createRestApi() {
		ParameterBuilder tokenPar1 = new ParameterBuilder();
        List<springfox.documentation.service.Parameter> pars = new ArrayList<>();
        tokenPar1.name("content-Type").description("Content-Type").defaultValue("application/json;charset=UTF-8").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        ParameterBuilder tokenPar2 = new ParameterBuilder();
        tokenPar2.name("accept").description("Accept").defaultValue("application/vnd.iOS.lshy-1.0+json").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        ParameterBuilder tokenPar3 = new ParameterBuilder();
        tokenPar3.name("token").description("token").defaultValue("29F7B").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        ParameterBuilder tokenPar4 = new ParameterBuilder();
        tokenPar4.name("App-Version").description("App-Version").defaultValue("1.0.0").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//        pars.add(tokenPar1.build());
        pars.add(tokenPar2.build());
        pars.add(tokenPar3.build());
        pars.add(tokenPar4.build());
		 //添加head参数配置start
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enableSwagger).select()
				.apis(RequestHandlerSelectors.basePackage("com.lsbc.pay")).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	@SuppressWarnings("deprecation")
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("license server").contact("yiyang").version("1.0.0").build();
	}

	/*
	 * 手动配置@EnableMvc默认的静态资源路径
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
}